package server;

import database.DatabaseConnection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import tools.DateUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class AutoExecuteTask {
    private static final Logger log = LogManager.getLogger();
    private static int[] missionList;

    static {
        missionList = new int[]{100100};
    }

    public static void execute() {

        // 每日0点自动执行的任务
        Timer.WorldTimer.getInstance().register(() -> {

            for (int missionId : missionList) {
                // 自动清除环任务
                try (Connection con = DatabaseConnection.getConnection()) {
                    try (PreparedStatement ps = con.prepareStatement("DELETE FROM missionstatus WHERE missionid = ?")) {
                        ps.setInt(1, missionId);
                        ps.executeUpdate();
                    }
                } catch (SQLException e) {
                    log.error("自动清除任务数据失败", e);
                }
            }

        }, 1000 * 60 * 60 * 24, DateUtil.getNextDayDiff(1));
    }
}
